import os

#python -u "d:\yurianime\ReplaceResourcePageswithMagnetLinks.py"

path = "D:\\yurianime\\"
f1 = open(path+"YuriAnimeResourcesClassify.csv","r",encoding="utf-8")
lines = f1.readlines()
f1.close()
files = os.listdir(path+"YuriAnimeResourcesPages\\")
f3 = open(path+"YuriAnimeResourcesClassifyMagnet.csv","w",encoding="utf-8")
for line in lines:
    if "miobt" not in line:
        f3.write(line)
    else:
        for file in files:
            if file in line[line.find("http"):len(line)-1]:
                f2 = open(path+"YuriAnimeResourcesPages\\"+file,"r",encoding="utf-8") 
                FileLines = f2.readlines()
                f2.close()
                for FileLine in FileLines:
                    if "magnet:?xt" in FileLine and "announce" in FileLine:
                        f3.write(line[:line.find("http")]+FileLine[FileLine.find("magnet:?xt"):FileLine.find("announce")+8]+"\n")
                        break
                break
f3.close()